<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class GradeController extends CommonController {
    //列表
    public function index(){
      $grade_object = D('user_grade');
      $map['status'] = array('egt', '0'); // 禁用和正常状态
      $Page = new Page($grade_object->where($map)->count(),C('ADMIN_CONFIG.page_size'));
      $Page->setConfig('prev','«');
      $Page->setConfig('next','»');
      $data_list     = $grade_object
          ->where($map)
          ->order('id asc')
          ->limit($Page->firstRow.','.$Page->listRows)
          ->select();
      $this->assign('mark','user');
      $this->assign('umark','grade');
      A('list')->setMetaTitle('等级列表')
          ->setBreadCrumb(array('模块','白帽管理','等级列表'))
          ->addTopButton('add',array('title'=>'等级添加'))
          ->addTopButton('resume')
          ->addTopButton('delete')
          ->addTableColumn('id','ID','number',50)
          ->addTableColumn('medal','勋章','image',40,'false')
          ->addTableColumn('title','名称','text',100,'false')
          ->addTableColumn('duty_free','免税金额（元）','text','auto','false')
          ->addTableColumn('high_join','优先参加权','status',100)
          ->addTableColumn('has_gift','礼品定制','status',80)
          ->addTableColumn('year_tour','年度旅游','status',80)
          ->addTableColumn('status','状态','status',60)
          ->addTableColumn('ctime','创建时间','date',120)
          ->addTableColumn('','操作','rightbtn',200,'false')
          ->setTableDataList($data_list)
          ->setTableDataPage($Page->show())
          ->addRightButton('edit')
          ->addRightButton('forbid')
          ->addRightButton('delete')
          ->fetch();
    }
    //添加
    public function add(){
      $grade_object = D('user_grade');
      if (IS_POST) {
          $data = $grade_object->create();
          if ($data) {
              $id = $grade_object->add($data);
              if ($id) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'等级新增成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'等级新增失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$grade_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','user');
        $this->assign('umark','grade');
        A('form')->setMetaTitle('等级添加')
        ->setBreadCrumb(array('模块','白帽管理','等级列表','等级添加'))
        ->setPostUrl(U('add'))
        ->addFormItem('title','text','等级标题','',array(),array('required'=>true))
        ->addFormItem('medal','image','等级勋章')
        ->addFormItem('duty_free','text','免税金额','',array(),array('required'=>true))
        ->addFormItem('high_join','switch','高级项目优先参加权','',array('有','无'))
        ->addFormItem('has_gift','switch','定期定制身份礼品','',array('有','无'))
        ->addFormItem('year_tour','switch','年度旅游','',array('有','无'))
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData(array('duty_free'=>0,'high_join'=>0,'has_gift'=>0,'year_tour'=>0,'status'=>1,'sort'=>0))
        ->fetch();
      }
    }
    //编辑
    public function edit(){
      $grade_object = D('user_grade');
      if (IS_POST) {
          $data = $grade_object->create();
          if ($data) {
              $rs = $grade_object->save($data);
              if ($rs) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'等级编辑成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'等级编辑失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$grade_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','system');
        $this->assign('umark','nav');

        $id = I('get.ids',0,'intval');
        $form_data = $grade_object->find($id);
        A('form')->setMetaTitle('等级编辑')
        ->setBreadCrumb(array('模块','白帽管理','等级列表','等级编辑'))
        ->setPostUrl(U('edit'))
        ->addFormItem('id','hidden')
        ->addFormItem('title','text','等级标题','',array(),array('required'=>true))
        ->addFormItem('medal','image','等级勋章')
        ->addFormItem('duty_free','text','免税金额','',array(),array('required'=>true))
        ->addFormItem('high_join','switch','高级项目优先参加权','',array('有','无'))
        ->addFormItem('has_gift','switch','定期定制身份礼品','',array('有','无'))
        ->addFormItem('year_tour','switch','年度旅游','',array('有','无'))
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData($form_data)
        ->fetch();
      }
    }
    //状态修改
    public function setStatus(){
        if (IS_GET) {
          $ids = I('get.ids');
          $status = I('get.status');
          $grade_object = M('user_grade');
          $map = array(
            'id' => array('in',$ids),
          );
          $save = array(
            'status' => $status,
          );
          $rs = $grade_object->where($map)->save($save);
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'等级状态修改成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'等级状态修改失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
    //删除
    public function delete(){
        if (IS_GET) {
          $ids = I('get.ids');
          $object_object = M('$grade_object');
          $map = array(
            'id' => array('in',$ids),
          );
          $rs = $object_object->where($map)->delete();
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'等级删除成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'等级删除失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
}
